﻿app.factory('errorHttpInterceptor', ['$q', '$location', 'toastr', '$rootScope',
    function($q, $location, toastr, $rootScope) {
        return function(promise) {
            return promise.then(function(response) {
                return response;
            }, function (response) {
                if (response.status === 0) {
                    toastr.error('Server connection lost');
                } else if (response.status === 401) {
                    $rootScope.$broadcast('event:loginRequired');
                } else if (response.status >= 400 && response.status < 500) {
                    toastr.error('Server was unable to find' +
                        ' what you were looking for... Sorry!!');
                } else {
                    toastr.error('Error ' + response.status + ': ' + response.data);
                }
                return $q.reject(response);
            });
        };
    }]);